<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid'],'pid'=>0);
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_yxgz_goods_group',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'分类管理'),
			array('op'=>'edit','title'=>'添加分类'),
		),
		'list'=>array('data'=>'list','tip'=>'','list'=>array(			
			array('name'=>'title','title'=>'分类名称','type'=>'string'),
			array('name'=>'image','title'=>'分类图片','type'=>'image'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'status'),		
		),'action'=>array(
			array('type'=>'href','op'=>'list2','title'=>'子分类管理','show'=>array('pid'=>0)),
		)),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_yxgz_goods_group',array('id'=>$id));
	}else{
		$item=array();
	}
	$item['shifeeset']=json_decode($item['shifeeset'],true);
	$item['feeset']=json_decode($item['feeset'],true);
    $return['item']=$item;
	$return['status']=array('0'=>'禁用','1'=>'启用');
	$return['isdefault']=array('0'=>'否','1'=>'是');	
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'分类设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'分类管理'),
			array('op'=>'edit','title'=>'添加分类')
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'title','title'=>'分类名称','type'=>'string'),
					array('name'=>'image','title'=>'分类图片','type'=>'image'),
					array('name'=>'desc','title'=>'分类描述','type'=>'text'),
					array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'status'),	
				)
			),			
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit2'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_yxgz_goods_group',array('id'=>$id));
	}else{
		$item=array();
		if($_RQ['pid']){
			$item['pid']=intval($_RQ['pid']);
			$item['parenttitle']=pdo_getcolumn('jy_yxgz_goods_group',array('id'=>$item['pid']),'title');
		}else{
			exi('请选择分类添加子分类！','error');
		}
	}
	$item['shifeeset']=json_decode($item['shifeeset'],true);
	$item['feeset']=json_decode($item['feeset'],true);
	$item['yjfeeset1']=$item['shifeeset']['yjfeeset'];
	$item['tcfeeset1']=$item['shifeeset']['tcfeeset'];
	$item['yjfeeset2']=$item['feeset']['yjfeeset'];
	$item['tcfeeset2']=$item['feeset']['tcfeeset'];
    $return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'子分类设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'子分类管理'),
			array('op'=>'edit','title'=>'添加子分类')
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'parenttitle','title'=>'所属分类','type'=>'string','read'=>true),
					array('name'=>'title','title'=>'子分类名称','type'=>'string'),
					array('name'=>'image','title'=>'子分类图片','type'=>'image'),
					array('name'=>'desc','title'=>'子分类描述','type'=>'text'),
					array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'status'),	
				)
			),		
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array_fields(array('pid','title','image','status','paixu','desc'),$postdata);
	$id=intval($postdata['id']);
	if($data['pid']){
		$gourl='/goods_group/list2/pid='.$data['pid'];
	}else{
		$gourl='/goods_group/list/';
	}
	if($id){
		pdo_update('jy_yxgz_goods_group',$data,array('id'=>$id));
		exi('设置成功！','',$gourl);
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=time();
		pdo_insert('jy_yxgz_goods_group',$data);
		exi('添加成功！','',$gourl);
	}
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_yxgz_goods_group',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_yxgz_goods_group',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='list2'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$params['pid']=intval($_RQ['id'])?:intval($_RQ['pid']);
	$return['params']['pid'] = $params['pid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$parenttitle=pdo_getcolumn('jy_yxgz_goods_group',array('id'=>$params['pid']),'title');
	$parenttitle=$parenttitle?:'';
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_yxgz_goods_group',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'分类管理'),
			array('op'=>'list2','title'=>$parenttitle.'子分类列表'),
			array('op'=>'edit2','title'=>'添加子分类','query'=>'pid='.$params['pid']),
		),
		'list'=>array('data'=>'list','tip'=>'','edit'=>false,'list'=>array(			
			array('name'=>'title','title'=>'子分类名称','type'=>'string'),
			array('name'=>'image','title'=>'子分类图片','type'=>'image'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'status'),		
		),'action'=>array(
			array('type'=>'href','op'=>'edit2','title'=>'编辑'),
		))
	);
	$return['htmls']=$htmls;
	exi($return);
}
?>